
package com.x52im.mall.logic.shop;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.eva.framework.dto.DataFromServer;
import com.x52im.mall.CommonPaymentActivity;
import com.x52im.mall.IntentFactory;
import com.x52im.mall.PayPalConfig;
import com.x52im.mall.shop.dto.SO;
import com.x52im.rbchat.R;
import com.eva.android.widget.DataLoadableActivity;
import com.paypal.android.sdk.payments.PayPalService;

public class OrderConfirmResultActivity extends DataLoadableActivity
{

	private TextView viewOrderId;

	private TextView viewOrderCurrency;

	private TextView viewOrderAmount;
	
	private TextView viewNote;

	private Button payBtn;

	private Button listBtn;

	private SO so = null;

	@Override
	protected void initViews(Bundle savedInstanceState)
	{
		customeTitleBarResId = R.id.common_score_layout_confirm_result_titleBar;
		// 首先设置contentview
		setContentView(R.layout.common_mall_shop_layout_order_confirm_result);
		// 设置标题
		this.setTitle(R.string.common_mall_shop_order_confirm_result_title);
		// 各组件初始化
		viewOrderId = (TextView) this.findViewById(R.id.common_mall_shop_order_confirm_result_order_id);
		viewOrderCurrency = (TextView) this.findViewById(R.id.common_mall_shop_order_confirm_result_order_currency);
		viewOrderAmount = (TextView) this.findViewById(R.id.common_mall_shop_order_confirm_result_order_amount);
		viewNote = (TextView)this.findViewById(R.id.common_mall_shop_order_confirm_result_note);
		
		payBtn = (Button) this.findViewById(R.id.common_mall_shop_order_confirm_result_to_pay);
		listBtn = (Button) this.findViewById(R.id.common_mall_shop_order_confirm_result_to_check);

		viewNote.setText(Html.fromHtml($$(R.string.common_mall_shop_order_confirm_result_tip)));
		
		so = (SO) getIntent().getSerializableExtra("SO");
		viewOrderId.setText(so.getOrder_id());
		viewOrderCurrency.setText(so.getOrder_currency().equals("0") ? "$" : "￥");
		viewOrderAmount.setText(so.getOrder_total());

		// 启动PayPalService
		startService(PayPalConfig.getPayPalServiceIntent(OrderConfirmResultActivity.this));
	}

	@Override
	protected void initListeners()
	{
		payBtn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				startActivityForResult(IntentFactory.createPaymentActivityIntent(OrderConfirmResultActivity.this, so, null, CommonPaymentActivity.PAYPAL_PAYMENT_DEVICE) , 0);
			}
		});

		listBtn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(OrderConfirmResultActivity.this, OrderManageActivity.class);
				startActivity(intent);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(resultCode == Activity.RESULT_OK)
		{
			Toast.makeText(OrderConfirmResultActivity.this, R.string.common_mall_shop_order_confirm_result_pay_success, Toast.LENGTH_LONG).show();
			payBtn.setVisibility(View.GONE);
		}
		else
			Toast.makeText(OrderConfirmResultActivity.this, R.string.common_mall_shop_order_confirm_result_pay_faliure, Toast.LENGTH_LONG).show();
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eva.android.widget.DataLoadableActivity#queryData(java.lang.String
	 * [])
	 */
	@Override
	protected DataFromServer queryData(String... arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eva.android.widget.DataLoadableActivity#refreshToView(java.lang
	 * .Object)
	 */
	@Override
	protected void refreshToView(Object arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onDestroy()
	{
		stopService(new Intent(this, PayPalService.class));
		super.onDestroy();
	}
}
